package com.atguigu.gulimall.cart.controller;

import com.atguigu.gulimall.cart.interceptor.CartInterceptor;
import com.atguigu.gulimall.cart.service.CartService;
import com.atguigu.gulimall.cart.vo.CartItemVo;
import com.atguigu.gulimall.cart.vo.CartVo;
import com.atguigu.gulimall.cart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    CartService cartService;

//    @GetMapping("cart.html")
//    public String cartListPage(HttpSession session) {
//        Object attribute = session.getAttribute(AuthServerConstant.LOGIN_USER);
//        if (null == attribute) {
//            //没登录获取临时的购物车数据
//        } else {
//            //获取登录了的购物车
//        }
//        return "cartList";
//    }

    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItemVo> getCurrentUserCartItems() {
        return  cartService.getUserCartItems();
    }

    @DeleteMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num) {
        cartService.changeItemCount(skuId,num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/itemCheck")
    public String itemCheck(@RequestParam("skuId") Long skuId,@RequestParam("checked") Integer checked) {
        cartService.checkItem(skuId,checked);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        //1、快速得到用户信息，id，user-key
//        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
//        System.out.println(userInfoTo);

        CartVo cartVo = cartService.getCart();
        model.addAttribute("cart",cartVo);
        return "cartList";
    }

    /**
     * 添加商品到购物车
     *    attributes.addFlashAttribute(); 将数据放在session里面可以在页面取出，但是只能取一次
     *     attributes.addAttribute("skuId",skuId);  //重定向携带数据   会自动将数据放在url后面
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes attributes) throws ExecutionException, InterruptedException {
       CartItemVo cartItemVo = cartService.addToCart(skuId,num);
//       model.addAttribute("item",cartItemVo);
        attributes.addAttribute("skuId",skuId);  //重定向携带数据

//        return "success";      //转发的数据可放在请求域中，如果是重定向，也会自动在后面的地址栏中拼接上
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    /**  跳转到成功页面
     * 解决重复提交的问题
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,Model model) {
        //重定向到成功页面，再次查询购物车的数据即可
       CartItemVo itemVo =  cartService.getCartItem(skuId);
       model.addAttribute("item",itemVo);
       return "success";
    }
}
